/**
 * Objeto para generar "drop down button" al vuelo.
 * Autor: Sahib Alejandro Jaramillo Leo
 */
function DropdownButton(Properties)
{
  Properties = $.extend({}, this.DropdownButtonDefaultProperties, Properties);

  var $Dropdown = $('<ul>').addClass('dropdown-menu');
  var $BtnGroup = $('<div>').attr('class', 'btn-group');
  var $Btn      = $('<button>').attr({
    'class': 'btn ' + Properties.classes + ' dropdown-toggle',
    'data-toggle': 'dropdown'
  }).text(Properties.btn_text).append(' ', $('<span>').addClass('caret'));

  if(Properties.icon){
    $Btn.prepend( $('<i>').addClass(Properties.icon), ' ' );
  }
  
  // Titulo del dropdown
  if(Properties.dropdown_title){
    $Dropdown.append(
      $('<li>').text(Properties.dropdown_title).css({
        'text-align': 'center',
        'font-weight': 'bold',
        'border-bottom': 'solid 1px #CCC',
        'padding-bottom': '4px'
      })
    )
  }

  for(var i in Properties.options){
    
    var $Link = $('<a>').attr('href', '#').text(Properties.options[i].title);
    if(typeof Properties.options[i].onClick == 'function'){
      $Link.on('click', Properties.options[i].onClick);
    } else {
      $Link.on('click', function(e){ e.preventDefault(); });
    }
    $Dropdown.append( $('<li>').append($Link) );

  }

  $BtnGroup.append($Btn, $Dropdown);

  return $BtnGroup.get(0);
};

DropdownButton.prototype.DropdownButtonDefaultProperties = {
  btn_text: '',
  classes: '',
  icon: '',
  dropdown_title: '',
  options: [
    { title: 'Option 1', onClick: function(e){console.log('Option');} },
  ]
};
